package com.loctek.importexport.mvp.acitvity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loctek.importexport.R;
import com.loctek.importexport.base.BaseActivity;
import com.loctek.importexport.bean.getproductResponse;
import com.loctek.importexport.mvp.presenter.ScanningProductPersenter;
import com.loctek.importexport.mvp.view.ScaningProductView;
import com.loctek.importexport.util.ScreenUtil;

import butterknife.BindView;


/**
 * 扫描产品
 */
public class ScanningProductActivity extends BaseActivity<ScaningProductView, ScanningProductPersenter> implements ScaningProductView {


    @BindView(R.id.ed_product_code)
    EditText edProductCode;
    @BindView(R.id.iv_search_clear)
    ImageView ivSearchClear;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_scdptypename)
    TextView tvScdptypename;
    @BindView(R.id.iv_pcs)
    TextView ivPcs;
    @BindView(R.id.tv_barcode)
    TextView tvBarcode;
    @BindView(R.id.tv_hscode)
    TextView tvHscode;
    @BindView(R.id.tv_width)
    TextView tvWidth;
    @BindView(R.id.tv_height)
    TextView tvHeight;
    @BindView(R.id.tv_weight)
    TextView tvWeight;
    @BindView(R.id.tv_length)
    TextView tvLength;
    @BindView(R.id.rlyt_see_img)
    RelativeLayout rlytSeeImg;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_scanning_product;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        setTitle(getResources().getString(R.string.Scanned_goods));

        edProductCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    ivSearchClear.setVisibility(View.VISIBLE);
                    getPresenter().getproduct(editable.toString());
                } else {
                    ivSearchClear.setVisibility(View.GONE);
                }
            }
        });

        ivSearchClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edProductCode.setText("");
                clear();
            }
        });


        edProductCode.setFocusable(true);
        edProductCode.setFocusableInTouchMode(true);
        edProductCode.requestFocus();


    }


    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @NonNull
    @Override
    public ScanningProductPersenter createPresenter() {
        return new ScanningProductPersenter(getApp());
    }

    private void clear() {
        ivImg.setImageResource(R.color.color_f_2);
        tvName.setText("");
        tvScdptypename.setText("");
        ivPcs.setText("");
        tvBarcode.setText("");
        tvHscode.setText("");
        tvWidth.setText("");
        tvHeight.setText("");
        tvWeight.setText("");
        tvLength.setText("");
        rlytSeeImg.setOnClickListener(null);
    }

    @Override
    public void getproduct(final getproductResponse data) {

        LoadingImg(ivImg, ScreenUtil.dip2px(40), ScreenUtil.dip2px(40), data.data.imageurl);

        rlytSeeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ScanningProductActivity.this, SeeBigImgActivity.class);
                intent.putExtra("url", data.data.imageurl);
                startActivity(intent);
            }
        });

        tvName.setText(data.data.name + "");
        tvScdptypename.setText(data.data.scdptypename + "");
        ivPcs.setText(data.data.pcs + "");
        tvBarcode.setText(data.data.barcode + "");
        tvHscode.setText(data.data.hscode + "");
        tvWidth.setText(data.data.width + "");
        tvHeight.setText(data.data.height + "");
        tvWeight.setText(data.data.weight + "");
        tvLength.setText(data.data.length + "");

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("name:" + data.data.name + "\t  barcode:" + data.data.barcode + "\n");
        stringBuffer.append("hqnum:" + data.data.hqnum + "\t  hscode:" + data.data.hscode + "\n");
        stringBuffer.append("width:" + data.data.width + "\t  height:" + data.data.height + "\n");
        stringBuffer.append("weight:" + data.data.weight + "\t  length:" + data.data.length + "\n");
        stringBuffer.append("pcs:" + data.data.pcs + " \t  traynum:" + data.data.traynum + "\n");
    }

}
